package com.wangjiyuan.giftshuo.ui.fragment.listtop.other;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.base.BaseFragment;
import com.wangjiyuan.giftshuo.bean.listtopbean.commentbean.CommentBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.everydaybean.EveryDayBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.maybelikebean.MayBeLikeBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.singleproductbean.SingleProductBean;
import com.wangjiyuan.giftshuo.ui.activity.listtop.DetailsItemActivity;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.adapter.RecyclerViewAdapter;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.interfaces.MyItemListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 *
 * 与ViewPager绑定的Fragment
 *
 */
public class ListTopItemFragment extends BaseFragment
        implements ListTopContract.IListTopView, MyItemListener {

    private RecyclerViewAdapter adapter;
    private RecyclerView recyclerView;
    private ListTopContract.IListTopPresenter iListTopPresenter;
    private ProgressBar pb_item_lt_fm;
    private List<EveryDayBean.DataBean.ItemsBean> itemsBeanList;
    private List<Object> objectList = new ArrayList<>();
    private EveryDayBean.DataBean dataEveryDay;
    private int path;
    private Map<String, String> params = new HashMap<>();

    /**
     *
     * @param context
     *
     * 初始化数据
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    /**
     *
     * @param savedInstanceState
     *
     * 通过Bundle传值获取数据
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null){
            path = bundle.getInt("path");
            params = (Map<String, String>) bundle.getSerializable("params");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list_top_item, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_listTop_viewPager);
        pb_item_lt_fm = (ProgressBar) view.findViewById(R.id.pb_item_lt_fm);

    }

    /**
     *
     * @param savedInstanceState
     *
     * 1、为了防止Fragment复用数据叠加，每次删除后重新添加数据
     * 2、启动MVP
     * 3、设置RecyclerView布局，及和适配器的绑定
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        objectList.clear();
        LoadStart();
        initRecyclerView();

    }

    private void initRecyclerView() {
        adapter = new RecyclerViewAdapter(getContext(), objectList, this);
        GridLayoutManager layoutManager = new GridLayoutManager(
                getContext(),
                2);
        //合并第一行
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (adapter.getItemViewType(position) == RecyclerViewAdapter.HEADER){
                    return 2;
                }else {
                    return 1;
                }
            }
        });
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }
    private void LoadStart() {
        iListTopPresenter = new ListTopPresenter(this);
        iListTopPresenter.getEveryDayData(path, params);
    }

    /**
     *
     * @param path
     * @return
     *
     * 把要传的path路径绑定到bundle里再传出去
     */
    public static ListTopItemFragment newInstance(int path, Map<String, String> params) {

        Bundle bundle = new Bundle();
        bundle.putInt("path", path);
        bundle.putSerializable("params", (Serializable) params);
        ListTopItemFragment fragment = new ListTopItemFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 显示加载进度图片
     */
    @Override
    public void showProgress() {
        pb_item_lt_fm.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏加载进度图片
     */
    @Override
    public void hideProgress() {
        pb_item_lt_fm.setVisibility(View.GONE);
    }

    /**
     *
     * @param msg
     *
     * 显示错误信息
     */
    @Override
    public void showErrorInfo(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    /**
     *
     * @param everyDayBean
     *
     * 把网络数据加载到Object集合里，传给适配器
     */
    @Override
    public void getEveryDayData(EveryDayBean everyDayBean) {
        itemsBeanList = everyDayBean.getData().getItems();
        dataEveryDay = everyDayBean.getData();
        objectList.add(dataEveryDay);
        objectList.addAll(itemsBeanList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getMayBeLikeData(MayBeLikeBean mayBeLikeBean) {
    }

    @Override
    public void getCommentData(CommentBean commentBean) {
    }

    @Override
    public void getSingleProductData(SingleProductBean singleProductBean) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void getPosition(int position) {
        Intent intent = new Intent(getActivity(), DetailsItemActivity.class);
        intent.putExtra("itemId", itemsBeanList.get(position-1).getId());
        startActivity(intent);
    }
}
